package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.entity.UserZfbCount;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.UserZfbCountMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserZfbCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.voice.vo.request.AddZfbCountVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户支付宝账户表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-04-16 15:58:56
 */
@Service
public class UserZfbCountServiceImpl extends ServiceImpl<UserZfbCountMapper, UserZfbCount> implements UserZfbCountService {
    @Resource
    private UserZfbCountMapper userZfbCountMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> addZfbCount(Long userId, AddZfbCountVo requestVo) {
        UserZfbCount userZfbCount1 = userZfbCountMapper.selectOne(new LambdaQueryWrapper<UserZfbCount>()
                .eq(UserZfbCount::getUserTel, requestVo.getUserTel()));
        if (!CheckEmptyUtil.isEmpty(userZfbCount1)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ZFB_COUNT_TRL_EXIST);
        }
        UserZfbCount userZfbCount = new UserZfbCount();
        userZfbCount.setUserId(userId);
        userZfbCount.setUserName(requestVo.getUserName());
        userZfbCount.setUserTel(requestVo.getUserTel());
        int insert = userZfbCountMapper.insert(userZfbCount);
        return ApiRestResponse.success(insert);
    }

    @Override
    public ApiRestResponse<AddZfbCountVo> selectUserZfbCount(Long id) {
        UserZfbCount userZfbCount = userZfbCountMapper.selectOne(new LambdaQueryWrapper<UserZfbCount>()
                .eq(UserZfbCount::getUserId, id));
        AddZfbCountVo addZfbCountVo = new AddZfbCountVo();
        if (!CheckEmptyUtil.isEmpty(userZfbCount)) {
            addZfbCountVo.setUserName(userZfbCount.getUserName());
            addZfbCountVo.setUserTel(userZfbCount.getUserTel());
        }
        return ApiRestResponse.success(addZfbCountVo);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> updateUserZfbCount(Long id, AddZfbCountVo requestVo) {
        UserZfbCount userZfbCount = userZfbCountMapper.selectOne(new LambdaQueryWrapper<UserZfbCount>()
                .eq(UserZfbCount::getUserId, id));
        userZfbCount.setUserName(requestVo.getUserName());
        userZfbCount.setUserTel(requestVo.getUserTel());
        int update = userZfbCountMapper.updateById(userZfbCount);
        return ApiRestResponse.success(update);
    }
}
